/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tourmsystem.bookingmn.controller;

/**
 *
 * @author Thanh Nga
 */
//public class TableModel {
    import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
import javax.naming.spi.DirStateFactory.Result;
import javax.swing.table.AbstractTableModel;
public class TableModel extends AbstractTableModel
{

    private Vector Title; // chua thong tin tieu de cua cot
    private Vector Data;  // chua du lieu trong bang

    public TableModel(ResultSet rsData) throws SQLException
    {
        ResultSetMetaData rsMeta = rsData.getMetaData(); // doc metadata cua resultset
        int count = rsMeta.getColumnCount();  // xac dinh so field trong resultset

        Title = new Vector(count);
        Data = new Vector();

        // lap tuong ung so phan tu cua Title de lay ten tung cot trong bang
        for (int i = 1; i <= count; i++) {
            Title.addElement(rsMeta.getColumnName(i));
        }

        // lap tu record dau tien den cuoi resultset de lay du lieu add vao trong bang
        while(rsData.next())
        {
            // khai bao 1 objectvecto la datarow de chua du lieu doc tu bang
            Vector datarow = new Vector(count);
            // lap dua theo so cot trong bang de lay thong tin cua tung doi tuong bo vao trong datarow
            for (int i = 1; i <=count; i++) {
                datarow.addElement(rsData.getObject(i));
            }
            Data.addElement(datarow);
        }
    }
    // phuong thuc tra ve so hang cua bang
    public int getRowCount() {
        return Data.size();
    }
    // phuong thuc tra ve so cot cua bang
    public int getColumnCount() {
        return Title.size();
    }
    // tra ve gia tri cua từng ô trong bảng
    public Object getValueAt(int row, int column) {
       Vector rowData = (Vector)(Data.elementAt(row));
       return rowData.elementAt(column);
    }
    public String getColumnName(int column) {
        return (String) (Title.elementAt(column));
    }
}
